//package com.mc.wsdemo.spring;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cglib.beans.BeanMap;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.AbstractWebSocketHandler;
//
//import java.io.IOException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.atomic.AtomicInteger;
//
///**
// * web socket 主处理程序
// *
// * @author 未来
// */
//@Slf4j
//@Component
//public class MyWsHandler extends AbstractWebSocketHandler {
//    private static Map<String, SessionBean> sessionBeanMap;
//    private static AtomicInteger clientIdMaker;
//
//
//    static {
//        sessionBeanMap = new ConcurrentHashMap<>();
//        clientIdMaker = new AtomicInteger(1);
//    }
//
//    //连接建立
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        super.afterConnectionEstablished(session);
//        //clientIdMaker.getAndIncrement():自增
//        SessionBean sessionBean = new SessionBean(session, clientIdMaker.getAndIncrement());
//        sessionBeanMap.put(session.getId(), sessionBean);
//        log.info(sessionBeanMap.get(session.getId()).getClientId() + "建立了连接");
//        StringBuffer stringBuffer=new StringBuffer();
//        stringBuffer.append(sessionBeanMap.get(session.getId()).getClientId() + "进入了群聊<br/>");
//        sendMessage(sessionBeanMap,stringBuffer);
//    }
//
//    /**
//     * 收到消息发送消息
//     *
//     * @param session
//     * @param message
//     * @throws Exception
//     */
//    @Override
//    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        super.handleTextMessage(session, message);
//        log.info(sessionBeanMap.get(session.getId()).getClientId() + ":" + message.getPayload());
////        sendMessage(sessionBeanMap,session);
//        send(sessionBeanMap, session, message);
//    }
//
//    //传输异常
//    @Override
//    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
//        super.handleTransportError(session, exception);
//        if (session.isOpen()) {
//            session.close();
//        }
//        sessionBeanMap.remove(session.getId());
//    }
//
//    //连接关闭
//    @Override
//    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        super.afterConnectionClosed(session, status);
//        int clientId = sessionBeanMap.get(session.getId()).getClientId();
//        sessionBeanMap.remove(session.getId());
//        log.info(clientId + "关闭了连接");
//        // 构造一个带有样式的HTML字符串
//        StringBuffer stringBuffer=new StringBuffer();
////        stringBuffer.append("<div style='color:red;text-align:center;'>"+clientId+"退出了群聊"+"</div>");
//        stringBuffer.append("<p style='text-align:center; color:red;'>"); // 开始标签，设置样式为居中且红色
//        stringBuffer.append(clientId);
//        stringBuffer.append("退出了群聊");
//        stringBuffer.append("</p>");
////        stringBuffer.append(clientId + "退出了群聊<br/>");
//        sendMessage(sessionBeanMap,stringBuffer );
//    }
//
//    //    //每2s发送给客户端心跳消息
////    @Scheduled(fixedRate = 2000)
////    public void sendMsg() throws IOException {
////        for(String key:sessionBeanMap.keySet()){
////            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage("心跳"));
////        }
////    }
//
//    public void sendMessage(Map<String, SessionBean> sessionBeanMap,StringBuffer stringBuffer) {
//        for (String key : sessionBeanMap.keySet()) {
//            try {
//                sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(stringBuffer.toString()));
//            } catch (IOException e) {
//                log.error(e.getMessage());
//            }
//        }
//    }
//
//    public void send(Map<String, SessionBean> sessionBeanMap, WebSocketSession session, TextMessage message) {
//        String id = session.getId();
//        log.info("id:" + id);
//        StringBuffer stringBuffer=new StringBuffer();
//        //判断是否是本人
//        for (String key : sessionBeanMap.keySet()) {
//
//            if (key.equals(id)) {
//                stringBuffer.append("<p style='text-align:right; color:green;'>")
//                        .append(sessionBeanMap.get(session.getId()).getClientId() + ":" + message.getPayload() + "</p>");
//            } else {
//                stringBuffer.append("<p style='text-align:left; color:blue;'>")
//                        .append(sessionBeanMap.get(session.getId()).getClientId() + ":" + message.getPayload() + "</p>");
//            }
//            try {
//                sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(stringBuffer.toString()));
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        //群发消息
////        for (String key : sessionBeanMap.keySet()) {
////
////            if (!session.getId().equals(session.getId())) {
////                try {
////                    stringBuffer.append("<p style='text-align:right; color:blue;'>")
////                            .append(sessionBeanMap.get(session.getId()).getClientId() + ":" + message.getPayload() + "</p>");
////                    sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(stringBuffer.toString()));
////                } catch (IOException e) {
////                    log.error(e.getMessage());
////                }
////            } else {
////                try {
////                    stringBuffer.append("<p style='text-align:left; color:green;'>")
////                            .append(sessionBeanMap.get(session.getId()).getClientId() + ":" + message.getPayload() + "</p>");
////                    sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(stringBuffer.toString()));
////                } catch (IOException e) {
////                    log.error(e.getMessage());
////                }
////            }
////
////
////        }
//
//    }
//}